{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from numpy import sin,cos,pi\n",
    "from ipywidgets import interact, interactive, fixed\n",
    "import ipywidgets as widgets\n",
    "import time\n",
    "import math\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "T = 1.618033988749895\n",
    "r = 4.77\n",
    "zmin,zmax = -r,r\n",
    "xmin,xmax = -r,r\n",
    "ymin,ymax = -r,r\n",
    "Nx,Ny,Nz = 77,77,77\n",
    "\n",
    "x = np.linspace(xmin,xmax,Nx)\n",
    "y = np.linspace(ymin,ymax,Ny)\n",
    "z = np.linspace(zmin,zmax,Nz)\n",
    "x,y,z = np.meshgrid(x,y,z,indexing='ij')\n",
    "p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x)).astype(np.float32)\n",
    "\n",
    "iso = k3d.marching_cubes(p, level=0.0, color=k3d.nice_colors[0], bounds=[xmin, xmax, ymin, ymax, zmin, zmax])\n",
    "\n",
    "plot += iso\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iso.opacity = 0.7\n",
    "ax = np.logspace(0, np.log10(xmax - xmin + 1), Nx, endpoint=True) + xmin - 1\n",
    "ay = np.logspace(0, np.log10(ymax - ymin + 1), Nx, endpoint=True) + ymin - 1\n",
    "az = np.logspace(0, np.log10(zmax - zmin + 1), Nx, endpoint=True) + zmin - 1\n",
    "\n",
    "x,y,z = np.meshgrid(ax,ay,az,indexing='ij')\n",
    "p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x)).astype(np.float32)\n",
    "\n",
    "iso2 = k3d.marching_cubes(p, level=0.0, color=1,\n",
    "                          spacings_x=(ax[1:] - ax[:-1]).astype(np.float32),\n",
    "                          spacings_y=(ay[1:] - ay[:-1]).astype(np.float32),\n",
    "                          spacings_z=(az[1:] - az[:-1]).astype(np.float32),\n",
    "                          bounds=[xmin, xmax, ymin, ymax, zmin, zmax],\n",
    "                          opacity=0.15,\n",
    "                          wireframe=True)\n",
    "plot += iso2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = plot.get_auto_camera(0.5, 0, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
